package com.ctsi.ssdc.service.impl;

import com.ctsi.ssdc.entity.Excel08;
import com.ctsi.ssdc.entity.Excel08VO.Excel08VO;
import com.ctsi.ssdc.entity.Excel08VO.MonthProductVo;
import com.ctsi.ssdc.entity.IndustrialEconomicIndicators;
import com.ctsi.ssdc.mapper.Excel08Mapper;
import com.ctsi.ssdc.service.Excel08Service;
import com.ctsi.ssdc.utils.MathUtil;
import com.ctsi.ssdc.utils.TimeUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import javax.annotation.Resource;

/**
 * 8基础表工业分行业(产值能耗)(Excel08)表服务实现类
 *
 * @author makejava
 * @since 2023-03-15 15:03:46
 */
@Service("excel08Service")
public class Excel08ServiceImpl implements Excel08Service {
    @Resource
    private Excel08Mapper excel08Mapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Excel08 queryById(Long id) {
        return this.excel08Mapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param excel08 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Excel08> queryByPage(Excel08 excel08, PageRequest pageRequest) {
        long total = this.excel08Mapper.count(excel08);
        return new PageImpl<>(this.excel08Mapper.queryAllByLimit(excel08, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param excel08 实例对象
     * @return 实例对象
     */
    @Override
    public Excel08 insert(Excel08 excel08) {
        this.excel08Mapper.insert(excel08);
        return excel08;
    }

    /**
     * 修改数据
     *
     * @param excel08 实例对象
     * @return 实例对象
     */
    @Override
    public Excel08 update(Excel08 excel08) {
        this.excel08Mapper.update(excel08);
        return this.queryById(excel08.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.excel08Mapper.deleteById(id) > 0;
    }
    
     /**
     *  批量添加数据
     * @param list
     * @return 是否成功
     */
    @Override
    public boolean insertBatch(List<Excel08> list) {
        return this.excel08Mapper.insertBatch(list)>0;
    }
    /**
     * 根据法人或集团信息获取一个月的公司数据
     *
     * @param year
     * @param month
     * @param group
     * @return
     */
    @Override
    public List<Excel08VO> getInfo(String year, String month, Integer group) {
        List<Excel08VO> list = new ArrayList<>();
        List<Excel08> excelList = null;
        if (group.equals(0)) {
            // 集团
            List<String> companyNames = excel08Mapper.getCompanyName(year, month);
            for (String companyName : companyNames) {
                excelList = excel08Mapper.getInfo(year, month, companyName);
                Excel08VO excel08VO = Excel08VO.voBuilder(year, month, companyName, 0, excelList);
                list.add(excel08VO);
            }
        } else if (group.equals(1)) {
            //法人
            List<String> personNames = excel08Mapper.getPersonName(year, month);
            for (String personName : personNames) {
                excelList = excel08Mapper.getInfoPerson(year, month, personName);
                Excel08VO excel08VO = Excel08VO.voBuilder(year, month, personName, 1, excelList);
                list.add(excel08VO);
            }
        } else {
            return null;
        }
        //排序
        list = list.stream()
                .sorted(Comparator.comparing(Excel08VO::getGyczCurrentaccumulate, Comparator.reverseOrder()))
                .collect(Collectors.toList());
        return list;
    }

    /**
     * 获取一个月公司按照行业分类的数据
     * 规模以上工业分行业产值
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public List<Excel08VO> getInfoType(String year, String month) {
        List<Excel08> excel08List = excel08Mapper.getInfoSectorName(year, month);
        return Excel08VO.copyList(excel08List);
    }

    @Override
    public List<MonthProductVo> getInfoYear(String year, String name, Integer apiType) {
        List<MonthProductVo> info = null;
        switch (apiType) {
            case 0:
                // 集团
                info = excel08Mapper.getInfoYear(year, name);
                return info;
            case 1:
                // 法人
                info = excel08Mapper.getInfoYearPerson(year, name);
                return info;
            case 2:
                // 公司
                info = excel08Mapper.getInfoYearCompany(year, name);
                return info;
            default:
                // 抛出异常
                throw new RuntimeException("非正常api");
        }
    }

    @Override
    public List<Excel08VO> getKeyInfo(String year, String month) {
        return excel08Mapper.getKeyInfo(year, month);
    }

    /**
     * 获取规上工业总产值当月 + 累计 + 同比
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public IndustrialEconomicIndicators getIndustryOutputInfo(String year, String month) {
        IndustrialEconomicIndicators info = excel08Mapper.getIndustryOutputInfo(year, month);
        if (info != null) {
            info.setStatisticalTime(TimeUtil.date(year, month));
            info.setIndicators("#规上工业总产值");
        }
        return info;
    }

    /**
     * 综合能源消费量（吨标准煤）当月 + 累计 + 累计同比
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public IndustrialEconomicIndicators getEnergyConsumptionInfo(String year, String month) {
        IndustrialEconomicIndicators info = excel08Mapper.getEnergyConsumptionInfo(year, month);
        IndustrialEconomicIndicators energyConsumptionInfo = null;
        if (info == null) return null;
        if (month.equals("2")) {
            info.setThisMonth(info.getAccumulate());
        } else {
            energyConsumptionInfo = excel08Mapper.getEnergyConsumptionInfo(year, String.valueOf(Integer.parseInt(month) - 1));
        }
        // 计算当月
        if (energyConsumptionInfo != null) {
            info.setThisMonth(info.getAccumulate() - energyConsumptionInfo.getAccumulate());
        }

        info.setStatisticalTime(TimeUtil.date(year, month));
        info.setIndicators("综合能源消费量（吨标准煤）");

        return info;
    }

    /**
     * 规上工业用电量（万千瓦时）当月 + 累计 + 同比
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public IndustrialEconomicIndicators getIndustrialElectricityInfo(String year, String month) {
        IndustrialEconomicIndicators info = excel08Mapper.getIndustrialElectricityInfo(year, month);
        IndustrialEconomicIndicators industrialElectricityInfo = null;
        if (info == null) return null;
        if (!month.equals("2")) {
            industrialElectricityInfo = excel08Mapper.getIndustrialElectricityInfo(year, String.valueOf(Integer.parseInt(month) - 1));
        } else {
            info.setThisMonth(info.getAccumulate());
        }
        // 计算当月
        if (industrialElectricityInfo != null) {
            info.setThisMonth(info.getAccumulate() - industrialElectricityInfo.getAccumulate());
        }
        info.setStatisticalTime(TimeUtil.date(year, month));
        info.setIndicators("规上工业用电量（万千瓦时）");
        return info;
    }

    /**
     * 获取万元产值能耗（吨标准煤/万元）当月 + 累计 + 同比
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public IndustrialEconomicIndicators getEnergyConsumption1wInfo(String year, String month) {
        IndustrialEconomicIndicators info = excel08Mapper.getEnergyConsumption1wInfo(year, month);

        // 万元产值能耗（吨标准煤/万元）当月 = 综合能源消费量（吨标准煤）当月/规上工业总产值当月
        IndustrialEconomicIndicators energyConsumptionInfo = getEnergyConsumptionInfo(year, month);
        IndustrialEconomicIndicators industryOutputInfo = getIndustryOutputInfo(year, month);
        if (info == null) return null;
        // 修改当月
        if (energyConsumptionInfo.getThisMonth() != null && industryOutputInfo.getThisMonth() != null) {
            double v = energyConsumptionInfo.getThisMonth() / industryOutputInfo.getThisMonth() / 10000;
            v = MathUtil.round(v,4); //四位小数
            info.setThisMonth(v);
        }
        info.setStatisticalTime(TimeUtil.date(year,month));
        info.setIndicators("万元产值能耗（吨标准煤/万元）");

        return info;
    }

    /**
     * 获取高新技术产业产值当月 + 累计 + 同比
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public IndustrialEconomicIndicators getHighTechInfo(String year, String month) {
        IndustrialEconomicIndicators info = excel08Mapper.getHighTechInfo(year, month);
        if (info == null) return null;
        info.setIndicators("高新技术产业产值");
        info.setStatisticalTime(TimeUtil.date(year,month));
        return info;
    }

    /**
     * 获取高新占比当月 + 累计 + 同比
     * @param year
     * @param month
     * @return
     */
    @Override
    public IndustrialEconomicIndicators getHighTechProportionInfo(String year, String month) {
        IndustrialEconomicIndicators info = excel08Mapper.getHighTechProportionInfo(year, month);
        if (info == null) return null;
        info.setIndicators("高新占比 %");
        info.setStatisticalTime(TimeUtil.date(year,month));
        return info;
    }

    /**
     * 根据日期删除
     * @param date
     * @return 是否成功
     */
    @Override
    public boolean removeByDate(Date date) {
        return excel08Mapper.removeByDate(date);
    }
}
